package services;

import helpers.StringHelper;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.EmailData;

import entities.Email;

@SuppressWarnings("serial")
public class SendEmailAddress extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String emails = (String) request.getParameter("emails");
		
		String[] lst = emails.split(",");
		
		for (int i = 0; i < lst.length; i++) {
			Email email = EmailData.getByEmail(lst[i]);
			if (email == null) {
				email = new Email();
				email.setEmail(lst[i]);
				email.setStatus(1);
				
				EmailData.insert(email);
			}
		}
		
		request.setAttribute("success", "Send success.");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String emails = (String) request.getParameter("emails");
		
		String[] lst = emails.split(",");
		
		for (int i = 0; i < lst.length; i++) {
			Email email = EmailData.getByEmail(lst[i]);
			if (email == null) {
				email = new Email();
				email.setEmail(lst[i]);
				email.setStatus(1);
				
				EmailData.insert(email);
			}
		}
		
		request.setAttribute("success", "Send success.");
	}
}
